package com.huanglj.himalaya.presenters;

import android.content.SharedPreferences;

import com.huanglj.himalaya.data.API.XimalayaAPI;
import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.interfaces.ISearchCallBack;
import com.huanglj.himalaya.interfaces.ISerachPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

import static com.huanglj.himalaya.utils.Constants.PAGE_SEARCH;

public class SearchPresenter implements ISerachPresenter {

    private static final String TAG = "SearchPresenter";
    private SharedPreferences mHotTopsp = null;
    private String mCurrentKeyword = null;
    private final XimalayaAPI mXimalayaAPI;
    private List<Album> mLists;


    private SearchPresenter(){
        mXimalayaAPI = XimalayaAPI.getInstance();
        if (BaseApplication.getContext()!=null) {
        }
    }

    private static SearchPresenter sSearchPresenter = null;

    public static SearchPresenter getInstance(){
        if (sSearchPresenter == null) {
            synchronized (SearchPresenter.class){
                if (sSearchPresenter == null) {
                    sSearchPresenter = new SearchPresenter();

                }
            }
        }
        return sSearchPresenter;
    }

    List<ISearchCallBack> mCallBacks = new ArrayList<>();
    @Override
    public void doSearch(String keyword) {
        mLists = new ArrayList<>();
        mLists.clear();
        mCurrentKeyword = keyword;
        search();
    }

    private void search() {
        mXimalayaAPI.searchByKeyWord(new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {
                List<Album> albums = searchAlbumList.getAlbums();
                for (ISearchCallBack callBack : mCallBacks) {
                    if (albums.size() != 0 ){

                        mLists.addAll(albums);
                        callBack.onSearchResultLoaded(mLists);
                    }else {
                        callBack.onEmpty();
                    }

                }


            }

            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG,"errorCode: " + i + ",errorMsg: " + s);
                for (ISearchCallBack callBack : mCallBacks) {
                    callBack.onError(i,s);
                }
            }
        },mCurrentKeyword,PAGE_SEARCH);
    }

    @Override
    public void reSearch() {
        search();
    }

    @Override
    public void loadMore() {
        PAGE_SEARCH ++;
        search();
    }

    @Override
    public void getHotWord() {
        mXimalayaAPI.searchByHotWord(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                List<HotWord> wordList = hotWordList.getHotWordList();

                for (ISearchCallBack callBack : mCallBacks) {
                    LogUtil.d(TAG,wordList.size()+"");
                    callBack.onHotWordLoaded(wordList);
                }

            }

            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG,"errorCode: " + i + ",errorMsg: " + s);
            }
        });
    }


    @Override
    public void getRecommendWord(String keyword) {
        mXimalayaAPI.getByRecommend(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                List<QueryResult> keyWordList = suggestWords.getKeyWordList();
                for (ISearchCallBack callBack : mCallBacks) {
                    callBack.onRecommendWordLoader(keyWordList);
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG,"errorCode: " + i + ",errorMsg: " + s);
            }
        });
    }

    @Override
    public void unRegisterViewCallBack(ISearchCallBack iSearchCallBack) {
        mCallBacks.remove(iSearchCallBack);
    }

    @Override
    public void registerViewCallBack(ISearchCallBack iSearchCallBack) {
        if (!mCallBacks.contains(iSearchCallBack)) {
            mCallBacks.add(iSearchCallBack);
        }
    }
}
